var chart;
var chartData = [];
var base = '';
AmCharts.ready(function() {
		chart = new AmCharts.AmSerialChart();
		chart.pathToImages = base+"/res/comm/js/amcharts/images/";
		chart.zoomOutButton = {
			backgroundColor : '#000000',
			backgroundAlpha : 0.15
		};
		chart.dataProvider = chartData;
		chart.categoryField = "date";
		chart.addListener("dataUpdated", zoomChart);
		var categoryAxis = chart.categoryAxis;
		categoryAxis.dashLength = 2;
		categoryAxis.gridAlpha = 0.15;
		categoryAxis.axisColor = "#DADADA";
		categoryAxis.dateFormats = [ {
			period : "DD",
			format : "YYYY-MM-DD"
		}, {
			period : "MM",
			format : "MMM"
		} ];
		var valueAxis1 = new AmCharts.ValueAxis();
		valueAxis1.axisColor = "#FF6600";
		valueAxis1.axisThickness = 2;
		valueAxis1.gridAlpha = 0;
		valueAxis1.title = "成交量";
		chart.addValueAxis(valueAxis1);
		
		var graph1 = new AmCharts.AmGraph();
		graph1.title = "成交量";
		graph1.balloonText = "当前成交量:[[cur_buy]]";
		graph1.valueField = "cur_buy";
		graph1.bullet = "round";
		graph1.hideBulletsCount = 30;
		chart.addGraph(graph1);

//		var graph2 = new AmCharts.AmGraph();
//		graph2.title = "卖出额";
//		graph2.balloonText = "卖出额:[[cur_sell]]";
//		graph2.valueField = "cur_sell";
//		graph2.bullet = "round";
//		graph2.hideBulletsCount = 30;
//		chart.addGraph(graph2);

		var chartCursor = new AmCharts.ChartCursor();
		chartCursor.cursorPosition = "mouse";
		chart.addChartCursor(chartCursor);

		var chartScrollbar = new AmCharts.ChartScrollbar();
		chart.addChartScrollbar(chartScrollbar);

//		var legend = new AmCharts.AmLegend();
//		legend.marginLeft = 110;
//		chart.addLegend(legend);
		//chart.addLabel(100, 20, "Beer Consumption by country", "left", 15, "#000000", 0, 1, true);

		chart.write("chartdiv");
		chartData.splice(0,chartData.length);
});

function zoomChart() {
   chart.zoomToIndexes(chartData.length -30, chartData.length -1);
}

function trendReload(type){
	ajaxPost(base+'/f/trade!trend_m.action',{'type':type},null,'json',trendcallback);
}

function trendcallback(data){
	if(data.status == 'success'){
		$('#cur_info').html('当前市场价:'+data.cur_pr+'		当前成交量:'+data.cur_buy);
		var his_buy_sell = data.his_buy_sell;
		if(his_buy_sell){
			var arr = eval(his_buy_sell);
			var size = arr.length;
			var d;
			for(var j = size-1 ; j >=0 ;j--){
				chartData.push({
					date : arr[j][0],
					cur_buy:arr[j][1]
				});
			}
	        //chart.dataProvider = chartData;
	        chart.validateData();
	        chartData.splice(0,chartData.length);
        }
	}else{
		//数据加载失败
	}
}